<?php
/*
 * wishingWall 0.0.3 - _pad.php
 *
 * Copyright (c) 2008 DeltaCat (http://www.zu14.cn)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $Date: 2008-12-6 14:22:17 -0400 (Sat, 6 Dec 2008) $
 */
 ?>
<?php

    define( 'WALL_LEFT_LIMIT', 30 );    //墙帖的屏幕左侧边界值
	define( 'WALL_TOP_LIMIT', 100 );     //墙帖的屏幕上边界值
	define( 'STYLE_COUNT', 8 );         //样式总数，自带8种样式
	define( 'IMAGE_COUNT', 15 );        //附图总数，自带15个，大家可以自己添加，同时需要修改此值
    //墙帖模板，轻易勿动
	define( 'WALL_PAD_TEMPLATE', '<div class="wall" style="z-index:{0};left:{1}px;top:{2}px;"><div class="top-{3}"><span class="x">×</span>第[{0}]条&nbsp;&nbsp;{4}</div><div class="mid-{3}"><span class="wt"><b>{5}</b>：</span><span>{6}</span></div><div class="bot-{3}"><img src="images/style/{7}.gif" alt="" /><span class="inline">{8}</span></div></div>' );

    /*---------------------使用前，请先配置下面的mysql信息--------------------*/
    define( 'MYSQL_USER', 'root' );
    define( 'MYSQL_DB', 'zhaoren' );
    define( 'MYSQL_PASS', '111111' );

    /*------------------------如果不熟悉PHP，请勿动下面的部分------------------------------*/

	function reload()
    {
		//echo('<script type="text/javascript">window.location.replace(\'default.php?w=\' + window.screen.availWidth + \'&h=\' + window.screen.availHeight);</script>');
		echo('<script type="text/javascript">window.location.replace(\'default.php?w=940&h=\' + window.screen.availHeight);</script>');
        die();
	}

    function apply_filter($str)
    {
        if(!get_magic_quotes_gpc())
        {
            $str = addslashes($str);
        }

        return htmlspecialchars($str);
    }

    class Pad
    {
        private $cn = 0;
        private function connect($user = MYSQL_USER, $pass = MYSQL_PASS, $db = MYSQL_DB, $host = 'localhost')
        {
            $this->cn = @mysql_connect($host, $user, $pass) or die(mysql_error());
            mysql_query("SET NAMES 'utf8'", $this->cn);
            mysql_select_db($db, $this->cn);
        }

        private function close()
        {
            mysql_close($this->cn);
        }

        function post($wishTo, $wishFrom, $wish)
        {
            $style = rand(1, STYLE_COUNT);
            $image = rand(1, IMAGE_COUNT);
            $wishFrom = apply_filter($wishFrom);
            $wishTo = apply_filter($wishTo);
            $wish = apply_filter($wish);

            $strSQL = "INSERT INTO wish (wFrom, wTo, wish, wStyleIndex, wImageIndex) VALUES ('{$wishFrom}', '{$wishTo}', '{$wish}', {$style}, {$image})";

            $this->connect();
            @mysql_query($strSQL, $this->cn) or die(mysql_error());
            $this->close();
        }

        function toHTML($screenWidth, $screenHeight)
        {
            $i = 1;
            $this->connect();
            $rs = @mysql_query('SELECT wFrom, wTo, wish, wTime, wStyleIndex, wImageIndex FROM wish ORDER BY wID ASC', $this->cn);

            while($row = mysql_fetch_row($rs))
            {
                $padleft = rand(1, $screenWidth - 250 - WALL_LEFT_LIMIT);
                $padtop = rand(WALL_TOP_LIMIT, $screenHeight - 350 - WALL_TOP_LIMIT);

                $strTemp = str_replace('{0}', strval($i), WALL_PAD_TEMPLATE);
                $strTemp = str_replace('{1}', strval($padleft), $strTemp);
                $strTemp = str_replace('{2}', strval($padtop), $strTemp);
                $strTemp = str_replace('{3}', $row[4], $strTemp);
                $strTemp = str_replace('{4}', $row[3], $strTemp);
                $strTemp = str_replace('{5}', $row[1], $strTemp);
                $strTemp = str_replace('{6}', $row[2], $strTemp);
                $strTemp = str_replace('{7}', $row[5], $strTemp);
                $strTemp = str_replace('{8}', $row[0], $strTemp);

                echo ($strTemp);

                $i++;
            }

            @mysql_free_result($rs);
            $this->close();
        }
    }
?>